﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// Summary description for clsUsersRecord
/// </summary>
public class clsUsersRecord
{
    #region MEMBER VARIABLES

        private int m_iUserID;
        private DateTime m_dtAdded;
        private int m_iAddedBy;
        private DateTime m_dtEdited;
        private int m_iEditedBy;
        private String m_strFirstName;
        private String m_strSurname;
        private String m_strPassword;
        private String m_strEmailAddress;
        private bool m_bIsActive;
        protected clsUser m_clsUser; 
    
    #endregion

    #region PROPERTIES

    public int iUserID
    {
        get
        {
            return m_iUserID;
        }
    }

    public DateTime dtAdded
    {
        get
        {
            return m_dtAdded;
        }
        set
        {
            m_dtAdded = value;
        }
    }

    public int iAddedBy
    {
        get
        {
            return m_iAddedBy;
        }
        set
        {
            m_iAddedBy = value;
        }
    }

    public DateTime dtEdited
    {
        get
        {
            return m_dtEdited;
        }
        set
        {
            m_dtEdited = value;
        }
    }

    public int iEditedBy
    {
        get
        {
            return m_iEditedBy;
        }
        set
        {
            m_iEditedBy = value;
        }
    }

    public String strFirstName
    {
        get
        {
            return m_strFirstName;
        }
        set
        {
            m_strFirstName = value;
        }
    }

    public String strSurname
    {
        get
        {
            return m_strSurname;
        }
        set
        {
            m_strSurname = value;
        }
    }

    public String strPassword
    {
        get
        {
            return m_strPassword;
        }
        set
        {
            m_strPassword = value;
        }
    }

    public String strEmailAddress
    {
        get
        {
            return m_strEmailAddress;
        }
        set
        {
            m_strEmailAddress = value;
        }
    }

    public bool bIsActive
    {
        get
        {
            return m_bIsActive;
        }
        set
        {
            m_bIsActive = value;
        }
    }

    #endregion

    #region CONSTRUCTORS

    public clsUsersRecord(clsUser clsUser)
    {
        m_clsUser = clsUser;
        m_iUserID = 0;
    }

    public clsUsersRecord(clsUser clsUser, int iUserID)
        : this(clsUser)
    {
        m_iUserID = iUserID;
        GetData();
    }

    #endregion

    #region PUBLIC METHODS

    public virtual void Update()
    {
        try
        {
            clsCommonFunctions clsCommonFunctions = new clsCommonFunctions();
            if (iUserID == 0)
            {
                //### Add
                m_iUserID = (int)DataConnection.GetDataObject().ExecuteScalar("spUsersInsert '" +
                    this.m_dtAdded + "'," + this.m_iAddedBy + " ,'" + this.m_dtEdited + "', " + this.m_iEditedBy + 
                    ", '" + clsCommonFunctions.escapeChar(this.m_strFirstName) + 
                    "', '" + clsCommonFunctions.escapeChar(this.m_strSurname) +
                    "', '" + clsCommonFunctions.escapeChar(this.m_strEmailAddress) +
                    "', '" + clsCommonFunctions.escapeChar(this.m_strPassword) + "'");
            }
            else
            {
                //### Update
                DataConnection.GetDataObject().Execute("spUsersUpdate " + this.m_iUserID + 
                    " , '" + this.m_dtEdited +"', " + this.m_iEditedBy +
                    ", '" + clsCommonFunctions.escapeChar(this.m_strFirstName) +
                    "', '" + clsCommonFunctions.escapeChar(this.m_strSurname) +
                    "', '" + clsCommonFunctions.escapeChar(this.m_strEmailAddress) +
                    "', '" + clsCommonFunctions.escapeChar(this.m_strPassword) + "'");
            }
        }
        catch (Exception ex)
        {
            clsErrorLog.LogError(ex, this, m_clsUser, 95);
            throw ex;
        }
    }

    public virtual void Delete()
    {
        DataConnection.GetDataObject().Execute("spUsersDelete " + this.m_iUserID);
    }

    #endregion

    #region PROTECTED METHODS

    protected virtual void GetData()
    {
        try
        {
            DataSet dtsData;
            dtsData = new DataSet();
            dtsData = DataConnection.GetDataObject().GetData("Data", "spUsersGetRecord " + this.m_iUserID);
            m_dtAdded = Convert.ToDateTime(dtsData.Tables["Data"].Rows[0]["dtAdded"]);
            m_iAddedBy = Convert.ToInt32(dtsData.Tables["Data"].Rows[0]["iAddedBy"]);
            m_dtEdited = Convert.ToDateTime(dtsData.Tables["Data"].Rows[0]["dtEdited"]);
            m_iEditedBy = Convert.ToInt32(dtsData.Tables["Data"].Rows[0]["iEditedBy"]);
            m_strFirstName = dtsData.Tables["Data"].Rows[0]["strFirstName"].ToString();
            m_strSurname = dtsData.Tables["Data"].Rows[0]["strSurname"].ToString();
            m_strPassword = dtsData.Tables["Data"].Rows[0]["strPassword"].ToString();
            m_strEmailAddress = dtsData.Tables["Data"].Rows[0]["strEmailAddress"].ToString();
            m_bIsActive = Convert.ToBoolean(dtsData.Tables["Data"].Rows[0]["bIsActive"]);

        }
        catch (Exception ex)
        {
            clsErrorLog.LogError(ex, this, m_clsUser, 95);
            throw ex;
        }
    }

    #endregion
}
